import Taro, { Component } from '@tarojs/taro'
import { View, Image, Text } from '@tarojs/components';
import './personCenter.scss';
import configs from "../../configuration";
import { AtIcon, AtAvatar } from 'taro-ui'
import {request_good,wx_login,good_request_noverify} from "../../untils/request";
import person_imgs from '../../assets/images/Group_my.png'

export default class Index extends Component {

  config = {
    navigationBarTitleText: '个人中心'
  };

  constructor(props) {
    super(props);
    this.state = {
      name: '请先登录',
      person_img: configs.cdnserver +'global_icon_personcenter_14x16_@3x.png',
      hadlogin: false,
      items: [
        {
          icon: configs.cdnserver + 'personal_indent_22x22_@3x.png',
          url: "/pages/index/index",
          name: "订单信息"
        },
        {
          icon: configs.cdnserver + 'personal_feedback_22x22_@3x.png',
          url: "/pages/suggest/suggest",
          name: "建议反馈"
        }
      ],
    }
  }

  componentWillMount() {
    console.log(Taro.ENV_TYPE)
    console.log(Taro.getEnv())

  }

  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {
    var env = Taro.getEnv().toUpperCase()
    var self = this
    if( env === 'WEAPP' ){
      let accesstoken = Taro.getStorageSync('access_token')
      let refreshtoken = Taro.getStorageSync('refresh_token')
      let open_id = Taro.getStorageSync('open_id')
     if(accesstoken && refreshtoken && open_id){
       console.log('当前储存数据',accesstoken,open_id)
       var get_user_data_obj = {
         url: configs.apis['userinfo'],
         data: { 'app_id': configs.app_id },
         header: { Authorization:'Bearer ' + accesstoken }
       }
       request_good(get_user_data_obj).then(res => {
         self.setState({
           name: res.res.user_nickname,
           person_img: res.res.user_avatar,
           hadlogin: true
         })
         console.log('直接登录',res)
       }).catch(err => {
         console.log(err)
         Taro.showModal({
           title: '登录失败',
           content: '是否需要重新登陆',
         }).then(res => {
           if(res.confirm){
             wx_login(get_user_data_obj)
           }else {
             console.log('用户取消了再次登陆')
           }

         })
       })

     }else {
       console.log('直接进行登录请求');
       wx_login().then(fn => {
         let accesstoken = Taro.getStorageSync('access_token')
         var get_user_data_obj = {
           url: configs.apis['userinfo'],
           data: { 'app_id': configs.app_id },
           header: { Authorization:'Bearer ' + accesstoken }
         }
         fn(get_user_data_obj).then(res => {
           self.setState({
             name: res.res.user_nickname,
             person_img: res.res.user_avatar,
             hadlogin: true
           })
           console.log('最终结果',res)
         })
       })
     }

    }else {
      var self = this
      var token = Taro.getStorageSync('access_token')
      console.log(token)
      if(token){
        var get_user_data_obj = {
          url: configs.apis['userinfo'],
          data: { 'app_id': configs.app_id},
          header: { Authorization:'Bearer ' + token },
          identity: true
        }
        request_good(get_user_data_obj).then(res => {
          if (res.code != 0 ){
            console.log(res)
            self.setState({
              hadlogin: false
            })
          }else {
            self.setState({
              name: res.res.user_bind_phone,
              person_img: person_imgs,
              hadlogin: true
            })
          }
        });
      }
    }

  }

  componentDidHide() {}

  onLogin (url, e) {
    if(!this.state.hadlogin){
      console.log('跳登录')
      Taro.navigateTo({
        url: '/pages/login/login',
      })
    }else {
      if(url){
        console.log(url)
        Taro.navigateTo({
          url: url,
        })
      }
    }
  }

  render() {
    var { items, name, person_img } = this.state;
    var person_center_list = <View className="person_center_lists">
      {items.map((post,index) => <View key={index} className="person_center_list">
        <Image className="person_center_list_img" src={post.icon} />
        <View className="person_center_list_div" onClick={this.onLogin.bind( this, post.url )}>
          <Text className="person_center_list_name">{post.name}</Text>
          {/*<AtIcon value='chevron-right' size='26' color='#CCC'></AtIcon>*/}
        </View>

      </View>)}
    </View>;
    return <View className="personCenter">
      <View className="personCenter_content" onClick={this.onLogin.bind(this)}>
        <AtAvatar className="personCenter_img" circle image={person_img}></AtAvatar>
        <Text className="personCenter_name" >{name}</Text>
      </View>
      {person_center_list}
    </View>;
  }
}
